/*
 * $Id: QELogDocument.m 78 2009-06-28 01:59:38Z jon@bway.net $
 * Chalk
 *
 * Created 10/5/08
 * Copyright Jon Gordon
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to 
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#import "QELogDocument.h"
#import "QELogWindowController.h"
#import "QEADIFImporter.h"

#import "QEADIF.h"


@interface QELogDocument (private)
- (void)doADIFExport;
- (void)doADIFImport;
@end


@implementation QELogDocument
#pragma mark LIFECYCLE METHODS
- (id)init 
{
    NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    self = [super init];
    if (self != nil) {
        // initialization code
    }
    return self;
} // init

- (void)makeWindowControllers
{
    logWindowController = [[QELogWindowController alloc] init];
    [self addWindowController:logWindowController];
    [logWindowController release];
} // makeWindowControllers


#pragma mark ACTIONS
- (IBAction)adifExport:(id)sender
{
    NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
	[self doADIFExport];
} // adifExport:

- (IBAction)adifImport:(id)sender
{
    NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
	[self doADIFImport];
} // adifImport:


#pragma mark OPERATIONS
- (void)doADIFExport
{
    NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
} // doADIFExport

- (void)doADIFImport
{
    NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
	int result;
    NSArray *fileTypes = [NSArray arrayWithObjects:@"adi", @"ADI", @"adif", @"ADIF", nil];
    NSOpenPanel *oPanel = [NSOpenPanel openPanel];
	
    [oPanel setAllowsMultipleSelection:NO];
    result = [oPanel runModalForDirectory:NSHomeDirectory()
									 file:nil types:fileTypes];
    if (result == NSOKButton) {
		NSString *path = [[oPanel filenames] objectAtIndex:0];
        QEADIFFile *adifFile = [QEADIFFileMaker makeADIFFileFromFile:path error:nil];
        if (nil != adifFile) {
            NSLog(@"\nADIF IMPORT:\n%@\n\n", [adifFile dump]);
            NSArrayController *qsosController = [logWindowController qsosController];
            QEADIFImporter *importer = [[QEADIFImporter alloc] init];
            for (NSArray *record in [adifFile adifRecords]) {
                id tmpRecord = [importer importADIFRecord:record];
                if (nil == tmpRecord) {
                    NSLog(@"  adif record imported as nil");
                } else if (0 == [tmpRecord count]) {
                    NSLog(@"  adif record imported, but created no fields");
                } else {
                    NSLog(@"  record: %@", tmpRecord);
                    id newEntity = [qsosController newObject];
                    for (NSString *columnName in [tmpRecord allKeys]) {
                        [newEntity setValue:[tmpRecord valueForKey:columnName] forKey:columnName];
                    } // for
                } // else
            } // for
        } // if
    } else {
		NSLog(@"  import cancelled");
	} // else
} // doADIFImport
@end
